"""
    通过继承的方式创建进程
    如果子进程时守护进程，当父进程结束后，子进程会自动被终止
"""
import multiprocessing

class EricProcess(multiprocessing.Process):
    def __init__(self, input_lock, content):
        multiprocessing.Process.__init__(self)
        self.lock = input_lock
        self.content = content

    def run(self):
        with self.lock:
            with open("process_out.txt", "a", encoding="utf-8") as f:
                f.write(f'{self.content}\r\n')

if __name__ == '__main__':
    lock = multiprocessing.Lock()
    for i in range(10):
        process = EricProcess(lock, f'process_{i}')
        process.daemon = True
        process.start()
        process.join()

    print("process ended.")